//
//  WCBuildStep.m
//  WabbitCode
//
//  Created by William Towe on 1/31/09.
//  Copyright 2009 Revolution Software. All rights reserved.
//

#import "WCBuildStep.h"
#import "WCProject.h"
#import "WCBuildTarget.h"
#import "NSImage+WCExtensions.h"
#import "NSDictionary+WCExtensions.h"
#import "WCFile.h"
#import "WCBuildConfiguration.h"


#define kWCBSStepTypeKey @"stepType"
#define kWCBSFileKey @"file"
#define kWCBSCreateCodeListingKey @"createCodeListing"
#define kWCBSGetInfoWindowFrameKey @"getInfoWindowFrame"

static NSFileManager *fm = nil;
static NSWorkspace *sw = nil;

@implementation WCBuildStep
#pragma mark *** Subclass Overrides ***
+ (void)initialize {
	fm = [NSFileManager defaultManager];
	sw = [NSWorkspace sharedWorkspace];
}

- (void)dealloc {

	[super dealloc];
}

- (BOOL)isLeaf {
	return YES;
}

- (BOOL)isValid {
	return [fm fileExistsAtPath:[self absoluteFilePath]];
}

- (NSImage *)icon {
	switch ([self stepType]) {
		case WCBSStepTypeAssembleFile:
			return [[self file] fileIcon];
		case WCBSStepTypeCreateLabelFile:
			return [NSImage imageNamed:@"CreateLabelFile16x16"];
		case WCBSStepTypeRunExecutableFile:
			return [NSImage imageNamed:@"RunExecutableFile16x16"];
		default:
			return nil;
	}
}

- (NSString *)nodeToolTip {
	switch ([self stepType]) {
		case WCBSStepTypeAssembleFile:
			return [NSString stringWithFormat:NSLocalizedString(@"Assemble File: \"%@\"",@"nodeToolTip for Assemble File Build Step"), [[self absoluteFilePath] stringByAbbreviatingWithTildeInPath]];
		case WCBSStepTypeCreateLabelFile:
			return [NSString stringWithFormat:NSLocalizedString(@"Create Label File: \"%@\"",@"nodeToolTip for Create Label File Build Step"), [[self absoluteFilePath] stringByAbbreviatingWithTildeInPath]];
		case WCBSStepTypeRunExecutableFile:
			return [NSString stringWithFormat:NSLocalizedString(@"Run Executable File: \"%@\"",@"nodeToolTip for Run Executable File Build Step"), [[self absoluteFilePath] stringByAbbreviatingWithTildeInPath]];
		default:
			return nil;
	}
}

- (BOOL)canCopyNodeOnDrop {
	return YES;
}

- (BOOL)isDragable {
	return YES;
}

#pragma mark NSCoding Protocol
- (id)initWithCoder:(NSCoder *)coder {
	if (!(self = [super initWithCoder:coder]))
		return nil;
	
	_stepType = [coder decodeIntegerForKey:kWCBSStepTypeKey];
	_file = [coder decodeObjectForKey:kWCBSFileKey];
	_getInfoWindowFrame = NSRectFromString([coder decodeObjectForKey:kWCBSGetInfoWindowFrameKey]);
	
	return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
	[super encodeWithCoder:coder];
	[coder encodeConditionalObject:_file forKey:kWCBSFileKey];
	[coder encodeInteger:_stepType forKey:kWCBSStepTypeKey];
	[coder encodeObject:NSStringFromRect(_getInfoWindowFrame) forKey:kWCBSGetInfoWindowFrameKey];
}
#pragma mark NSCopying Protocol
- (id)copyWithZone:(NSZone *)zone {
	WCBuildStep *copy = [super copyWithZone:zone];
	
	copy->_file = _file;
	copy->_stepType = _stepType;
	copy->_getInfoWindowFrame = _getInfoWindowFrame;
	
	return copy;
}
#pragma mark NSMutableCopying Protocol
- (id)mutableCopyWithZone:(NSZone *)zone {
	WCBuildStep *copy = [super mutableCopyWithZone:zone];
	
	copy->_file = _file;
	copy->_stepType = _stepType;
	copy->_getInfoWindowFrame = _getInfoWindowFrame;
	
	return copy;
}
#pragma mark *** Public Methods ***
#pragma mark Accessors
@synthesize stepType=_stepType, file=_file,getInfoWindowFrame=_getInfoWindowFrame;

@dynamic absoluteFilePath;
- (NSString *)absoluteFilePath {
	return [[self file] absoluteFilePath];
}

@dynamic spasmFlags;
- (NSArray *)spasmFlags {
	switch (_stepType) {
		case WCBSStepTypeCreateLabelFile:
			return [NSArray arrayWithObjects:@"-L", @"-O", nil];
		case WCBSStepTypeAssembleFile:
		case WCBSStepTypeRunExecutableFile:
		default:
			return [NSArray array];
	}
}

@dynamic outputFileNames;
- (NSArray *)outputFileNames {
	switch (_stepType) {
		case WCBSStepTypeAssembleFile:
			if ([[[self buildTarget] activeBuildConfiguration] createCodeListing]) {
				return [NSArray arrayWithObjects:[[[[self file] name] stringByDeletingPathExtension] stringByAppendingPathExtension:@"lst"], [[[[self file] name] stringByDeletingPathExtension] stringByAppendingPathExtension:[[self buildTarget] outputFileExtension]], nil];
			}
			return [NSArray arrayWithObject:[[[[self file] name] stringByDeletingPathExtension] stringByAppendingPathExtension:[[self buildTarget] outputFileExtension]]];
		case WCBSStepTypeCreateLabelFile:
			return [NSArray arrayWithObject:[[[[self file] name] stringByDeletingPathExtension] stringByAppendingPathExtension:@"lab"]];
		case WCBSStepTypeRunExecutableFile:
		default:
			return nil;
	}
}

@dynamic stepTypeAsString;
- (NSString *)stepTypeAsString {
	switch ([self stepType]) {
		case WCBSStepTypeAssembleFile:
			return [NSString stringWithFormat:NSLocalizedString(@"Assemble \"%@\"",@"Assemble \"%@\""), [[self file] name]];
		case WCBSStepTypeCreateLabelFile:
			return NSLocalizedString(@"Create Label File",@"Create Label File");
		case WCBSStepTypeRunExecutableFile:
			return NSLocalizedString(@"Run Executable File",@"Run Executable File");
		default:
			return nil;
	}
}

@dynamic buildTarget;
- (WCBuildTarget *)buildTarget {
	return (WCBuildTarget *)[self parentNode];
}
#pragma mark Creation
+ (id)buildStepWithStepType:(WCBSStepType)stepType file:(WCFile *)file; {
	return [[[[self class] alloc] initWithStepType:stepType file:file] autorelease];
}

- (id)initWithStepType:(WCBSStepType)stepType file:(WCFile *)file; {
	if (!(self = [super initWithNodeName:nil]))
		return nil;
	
	_stepType = stepType;
	_file = file;
	_name = [[self stepTypeAsString] copy];
	
	return self;
}
@end
